const path = require('path')

const config = {
  date: '2020-4-2',
  designWidth: 750,
  deviceRatio: {
    '640': 2.34 / 2,
    '750': 1,
    '828': 1.81 / 2
  },
  sourceRoot: 'src',
  outputRoot: 'dist',
  babel: {
    sourceMap: true,
    presets: [
      ['env', {
        modules: false
      }]
    ],
    plugins: [
      'transform-decorators-legacy',
      'transform-class-properties',
      'transform-object-rest-spread',
      ['transform-runtime', {
        'helpers': false,
        'polyfill': false,
        'regenerator': true,
        'moduleName': 'babel-runtime'
      }]
    ]
  },
  plugins: [
    '@tarojs/plugin-sass',
    '@tarojs/plugin-terser'
  ],
  sass: {
    resource: [
      'config/theme.scss',
      'src/styles/variable.scss',
    ]
  },
  //  用来配置一些全局变量供代码中进行使用，配置方式与 Webpack DefinePlugin 类似，例如：
  defineConstants: {},
  // copy: {
  //   patterns: [
  //     //  复制小程序配置文件
  //     { from: 'config.js', to: 'dist/config.js' }
  //   ]
  // },
  mini: {
    postcss: {
      pxtransform: {
        enable: true,
        config: {}
      },
      url: {
        enable: true,
        config: {
          limit: 10240 // 设定转换尺寸上限
        }
      },
      cssModules: {
        enable: false, // 默认为 false，如需使用 css modules 功能，则设为 true
        config: {
          namingPattern: 'module', // 转换模式，取值为 global/module
          generateScopedName: '[name]__[local]___[hash:base64:5]'
        }
      }
    }
  },
  h5: {
    devServer: {
      port: 10086
    },
    router: {
      mode: 'browser', // 或者是 [hash, browser]'
      // basename: '/myapp'
    },
    publicPath: '/',
    staticDirectory: 'static',
    postcss: {
      pxtransform: {
        enable: true,
        config: {
          platform: 'h5',
          designWidth: 750,
        }
      },
      autoprefixer: {
        enable: true,
        config: {
          browsers: [
            'last 3 versions',
            'Android >= 4.1',
            'ios >= 8'
          ]
        }
      },
      cssModules: {
        enable: true, // 默认为 false，如需使用 css modules 功能，则设为 true
        config: {
          namingPattern: 'module', // 转换模式，取值为 global/module
          generateScopedName: '[local]_[hash:base64:8]'
          // generateScopedName: '[hash:base64:8]'
        }
      }
    }
  },
  alias: {
    '@/api': path.resolve(__dirname, '..', 'src/api'),
    '@/assets': path.resolve(__dirname, '..', 'src/assets'),
    '@/components': path.resolve(__dirname, '..', 'src/components'),
    '@/config': path.resolve(__dirname, '..', 'src/config'),
    '@/market': path.resolve(__dirname, '..', 'src/market'),
    '@/styles': path.resolve(__dirname, '..', 'src/styles'),
    '@/utils': path.resolve(__dirname, '..', 'src/utils'),
  }
}

module.exports = function (merge) {
  if (process.env.NODE_ENV === 'development') {
    return merge({}, config, require('./dev'), require('./config'))
  }
  console.log('---------------------------------------------------- prod ----------------------------------------------------')
  console.log('打包完成后、需手动修改 config.js 的引入方式\r\n')
  return merge({}, config, require('./prod'), require('./config'))
}
